﻿static class ListNodeExtensions
{
    public static IEnumerable<int> AsEnumerable(this ListNode listNode)
    {
        for (var current = listNode; current != null; current = current.next)
            yield return current.val;
    }
    public static ListNode? ToListNodes(this IEnumerable<int> ints)
    {
        ListNode listNode = new ListNode();
        ListNode? last = null;
        var current = listNode;
        foreach (var item in ints)
        {
            current.val = item;
            last = current;
            current = current.next = new ListNode();
        }
        if (last != null)
        {
            last.next = null;
            return listNode;
        }
        else
            return null;
    }
}

/// <summary>
/// https://leetcode-cn.com/problems/merge-two-sorted-lists/
/// </summary>
public class Solution
{
    public ListNode? MergeTwoLists(ListNode list1, ListNode list2)
    {
        return list1.AsEnumerable().Concat(list2.AsEnumerable()).OrderBy(item => item).ToListNodes();
    }
}